﻿using Microsoft.EntityFrameworkCore;
using System.Diagnostics;
using TuLing.Dto;
using TuLing.Entities;
using TuLing.IServices;

namespace TuLing.Services
{
    [DebuggerDisplay($"{{{nameof(GetDebuggerDisplay)}(),nq}}")]
    public class ProductService : BaseService, IProductService
    {
        public ProductService(DefaultDbContext context) : base(context) { }

        /// <summary>
        /// 商品列表
        /// </summary> 
        public async Task<List<ProductDto>> Search(string keyword = "")
        {
            if (keyword == string.Empty)
            {
                var list1 = await _context.Products.ToListAsync();
                return list1.Select(m => new ProductDto
                {
                    Id = m.Id,
                    ProductCategoryId = m.ProductCategoryId,
                    Code = m.Code,
                    Name = m.Name,
                    Unit = m.Unit,
                    Price = m.Price,
                    Remark = m.Remark
                }).ToList();
            }
            else
            {
                var list = await _context.Products.Where(m => m.Name.Contains(keyword)).ToListAsync();
                return list.Select(m => new ProductDto
                {
                    Id = m.Id,
                    ProductCategoryId = m.ProductCategoryId,
                    Code = m.Code,
                    Name = m.Name,
                    Unit = m.Unit,
                    Price = m.Price,
                    Remark = m.Remark
                }).ToList();
            }
        }

        public async Task<ProductDto?> Get(long id)
        {
            var first = await _context.Products.FirstOrDefaultAsync(m => m.Id == id);

            // 如果没有找到商品，返回null
            if (first == null)
            {
                return null;
            }

            // 将实体转换为DTO
            return new ProductDto
            {
                Id = first.Id,
                ProductCategoryId = first.ProductCategoryId,
                Code = first.Code,
                Name = first.Name,
                Unit = first.Unit,
                Price = first.Price,
                Remark = first.Remark
            };
        }

        /// <summary>
        /// 保存商品
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task Save(ProductDto input)
        {
            var exist = await _context.Products.AnyAsync(m => m.Name == input.Name && m.Id != input.Id);

            // 检查商品名称是否存在
            if (exist)
            {
                throw new Exception("商品名已存在，请检查！");
            }
            else
            {
                // 检查商品编码是否存在                
                if (input.Id == 0)
                {
                    var product = new Product
                    {
                        ProductCategoryId = input.ProductCategoryId,
                        Code = input.Code,
                        Name = input.Name,
                        Unit = input.Unit,
                        Price = input.Price,
                        Remark = input.Remark
                    };
                    _context.Products.Add(product);
                    _context.SaveChanges();
                    input.Id = product.Id; // 获取新插入的商品ID
                }
                else
                {
                    var product = _context.Products.First(m => m.Id == input.Id);
                    product.ProductCategoryId = input.ProductCategoryId;
                    product.Code = input.Code;
                    product.Name = input.Name;
                    product.Unit = input.Unit;
                    product.Price = input.Price;
                    product.Remark = input.Remark;
                    _context.Products.Update(product);
                    _context.SaveChanges();
                }
            }
        }

        /// <summary>
        /// 删除商品
        /// </summary>  
        public async Task Delete(long id)
        {
            var first = await _context.Products.FirstAsync(m => m.Id == id);
            _context.Products.Remove(first);
            _context.SaveChanges();
        }

        private string GetDebuggerDisplay()
        {
            return ToString();
        }
    }
}
